﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Wasion.HHU.Services
{
    public class CommManager
    {
        //private static IWsComm commInterface = null;  //串口通信接口
        //private static IWsComm tcpcInterface = null;  //TCP客户端方式通信接口
        //private static IWsComm tcpsInterface = null;  //TCP服务端方式通信接口
        //private static IWsComm tcpCANInterface = null;  //TCP CAN总线方式通信接口    


        //private static IWsComm CreateInterface(CommProperty.CommMode commflag)                   
        //{
        //    IWsComm serviceitf = null;
        //    try
        //    {
        //        string dllname = string.Empty;
        //        string classname = string.Empty;
        //        switch (commflag)
        //        {
        //            case CommProperty.CommMode.RS232:
        //            case CommProperty.CommMode.OPTICAL: 
        //            case CommProperty.CommMode.OPTICAL1107:
        //            case CommProperty.CommMode.BLELOW:
        //            case CommProperty.CommMode.WSBLE:
        //                dllname = "WsComm.wsSerialPort";
        //                classname = ".Manager";
        //                break;
        //            case CommProperty.CommMode.TCPC:
        //            case CommProperty.CommMode.UDPC:
        //                dllname = "WsComm.wsSocket";
        //                classname = ".ManagerClient";
        //                break;
        //            case CommProperty.CommMode.TCPS:
        //                dllname = "WsComm.wsSocket";
        //                classname = ".ManagerServer";
        //                break;
        //            case CommProperty.CommMode.TCPCAN:
        //                dllname = "WsComm.wsSocket";
        //                classname = ".ManagerCAN";
        //                break;
        //        }
        //        string dllpath = Application.StartupPath
        //                       + "\\"
        //                       + dllname
        //                       + ".dll";
        //        Assembly asmb = Assembly.LoadFrom(dllpath);
        //        Type type = asmb.GetType(dllname + classname);
        //        serviceitf = (IWsComm)type.Assembly.CreateInstance(dllname + classname);
        //    }
        //    catch(Exception e)
        //    { 

        //    }
        //    return serviceitf;
        //}

        //private static IWsComm GetInterface(CommProperty.CommMode commflag)   
        //{
        //    IWsComm commintr = null;
        //    switch (commflag)
        //    {
        //        case CommProperty.CommMode.RS232:
        //        case CommProperty.CommMode.OPTICAL:
        //        case CommProperty.CommMode.OPTICAL1107:
        //        case CommProperty.CommMode.BLELOW:
        //        case CommProperty.CommMode.WSBLE:  
        //            commintr =  commInterface;
        //            break;
        //        case CommProperty.CommMode.TCPC:
        //        case CommProperty.CommMode.UDPC:
        //            commintr = tcpcInterface;
        //            break;
        //        case CommProperty.CommMode.TCPS:
        //            commintr = tcpsInterface;
        //            break;
        //        case CommProperty.CommMode.TCPCAN:
        //            commintr = tcpCANInterface;
        //            break;
        //    }
        //    return commintr;
        //}

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="errinfo">回传错误信息</param>
        //public static int InitCommInfo(CommProperty.CommMode commflag, ref string errinfo)
        //{
        //    int rs = 0;
        //    CommConst.StartUpPath = AppDomain.CurrentDomain.DynamicDirectory;

        //    switch (commflag)
        //    {
        //        case CommProperty.CommMode.RS232:
        //        case CommProperty.CommMode.OPTICAL:
        //        case CommProperty.CommMode.OPTICAL1107:
        //        case CommProperty.CommMode.BLELOW:
        //        case CommProperty.CommMode.WSBLE:
        //            commInterface = CreateInterface(commflag);
        //            if (commInterface != null)
        //                rs = commInterface.InitCommInfo(ref errinfo);
        //            break;
        //        case CommProperty.CommMode.TCPC:
        //        case CommProperty.CommMode.UDPC:
        //            tcpcInterface = CreateInterface(commflag);
        //            if (tcpcInterface != null)
        //                rs = tcpcInterface.InitCommInfo(ref errinfo);
        //            break;
        //        case CommProperty.CommMode.TCPS:
        //            tcpsInterface = CreateInterface(commflag);
        //            if (tcpsInterface != null)
        //                rs = tcpsInterface.InitCommInfo(ref errinfo);
        //            break;
        //        case CommProperty.CommMode.TCPCAN:
        //            tcpCANInterface = CreateInterface(commflag);
        //            if (tcpCANInterface != null)
        //                rs = tcpCANInterface.InitCommInfo(ref errinfo);
        //            break;
        //    }
        //    return rs;
        //}

        /// <summary>
        /// 初始化所有通信模块
        /// </summary>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int InitCommInfo(ref string errinfo)
        //{
        //    int rs = InitCommInfo(CommProperty.CommMode.RS232, ref errinfo);
        //    if (rs <= 0)
        //        return rs;
        //    rs = InitCommInfo(CommProperty.CommMode.TCPC, ref errinfo);
        //    if (rs <= 0)
        //        return rs;
        //    rs = InitCommInfo(CommProperty.CommMode.TCPS, ref errinfo);
        //    if (rs <= 0)
        //        return rs;
        //    rs = InitCommInfo(CommProperty.CommMode.TCPCAN, ref errinfo);
        //    return rs;
        //}

        /// <summary>
        /// 获取前导符
        /// </summary>
        /// <returns></returns>
        public static string GetWakeChar()
        {
            return CommConst.WakeChar;
        }

        /// <summary>
        /// 获取延时符
        /// </summary>
        /// <returns></returns>
        public static string GetDelayChar()
        {
            return CommConst.DelayChar;
        }


        /// <summary>
        /// 添加指令
        /// </summary>
        /// <param name="aOrder">指令类</param>
        /// <param name="maxovertime">回传该指令的最大等待时间</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int AddOrder(CommConst.TOrdertype aOrder, ref int maxovertime, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        //IWsComm commintr = GetInterface(aOrder.connMode);
        //        rs = commintr.AddOrder(aOrder, ref maxovertime, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}

        /// <summary>
        /// 启动发指令
        /// </summary>
        /// <param name="aOrder">指令类</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        public static int StartOrder(CommConst.TOrdertype aOrder, ref string errinfo)   
        {
            int rs = 0;
            try
            {
                //var commintr = new Manager();
                //rs = commintr.StartOrder(aOrder, ref errinfo);

            }
            catch (Exception e)
            {
                errinfo = e.Message;
            }
            return rs;
        }


        /// <summary>     
        /// 启动监听
        /// </summary>
        /// <param name="aOrder">指令类</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int StartListen(CommConst.TOrdertype aOrder, ref string errinfo)            
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(aOrder.connMode);
        //        rs = commintr.StartListen(aOrder, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}


        /// <summary>
        /// 上报帧确认  2019-8-19
        /// </summary>
        /// <param name="aOrder">指令类</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int UploadConfirm(CommConst.TOrdertype aOrder, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(aOrder.connMode);
        //        rs = commintr.UploadConfirm(aOrder, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}


        /// <summary>
        /// 停止发指令
        /// </summary>
        /// <param name="meteridOrcommname">表ID或串口号</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int StopOrder(CommMode connMode, string meteridOrcommname, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(connMode);
        //        rs = commintr.StopOrder(meteridOrcommname, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}


        /// <summary>
        /// 清空指令
        /// </summary>
        /// <param name="meteridOrcommname">表ID或串口号</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int ClearOrder(CommMode connMode, string meteridOrcommname, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(connMode);
        //        rs = commintr.ClearOrder(meteridOrcommname, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}



        /// <summary>
        /// 串口通信用于关闭串口，TCP串口用于断开TCP连接
        /// </summary>
        /// <param name="meteridOrcommname">表ID或串口号</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int CloseComm(CommMode connMode, string meteridOrcommname, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(connMode);
        //        rs = commintr.CloseComm(meteridOrcommname, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}


        /// <summary>
        /// 串口通信用于关闭和某个表的通信
        /// </summary>
        /// <param name="commname">串口号</param>
        /// <param name="meterid">表ID</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int CloseComm(CommProperty.CommMode connMode, string commname, string meterid, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        IWsComm commintr = GetInterface(connMode);
        //        rs = commintr.CloseComm(commname, meterid, ref errinfo);  
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}


        /// <summary>
        /// 刷新波特率
        /// </summary>
        /// <param name="aComm">串口号</param>
        /// <param name="newBaudRate">新波特率</param>
        /// <param name="errinfo">回传错误信息</param>
        /// <returns>大于0表示成功  小于0表示失败</returns>
        //public static int RefreshBaudRate(string aComm, int newBaudRate, ref string errinfo)
        //{
        //    int rs = 0;
        //    try
        //    {
        //        rs = commInterface.RefreshBaudRate(aComm, newBaudRate, ref errinfo);
        //    }
        //    catch (Exception e)
        //    {
        //        errinfo = e.Message;
        //    }
        //    return rs;
        //}
    }
}
